import os
import pygame
import json
from tools import *
from ui import filedialog
from translate import l
from config import config, uiconfig
from ui import window, events
from ui.menu import menu
from ui import src
screen = None
history_list = []


def loading_history():
    info("ui/start/startui", "loading history")
    if os.path.exists(config.DATA_DIR+config.HISTORY_DIR):
        history_path_list = []
        with open(config.DATA_DIR+config.HISTORY_DIR, "r") as history_file:
            history_path_list = history_file.readlines()
        history_path_list.reverse()
        for this_history_temp in history_path_list:
            this_history = this_history_temp[:-1]
            if os.path.exists(this_history+"/"+"manifest.json"):
                with open(this_history+"/"+"manifest.json", "r") as project_file:
                    try:
                        history_list.append(
                            (json.loads(project_file.read())["name"], this_history))
                    except:
                        wrn("ui/start/startui",
                            f"project [{this_history}] broken")
            else:
                wrn("ui/start/startui",
                    f"cannot find project [{this_history}]")


def render_startui(render_screen):
    global screen
    screen = render_screen

    # history
    pygame.draw.rect(screen, uiconfig.DRAWER_COLOR, (0, 0, 350, 600), 0)
    history_render_y = 10
    for this_history in history_list:
        if (pygame.Rect(350+10, history_render_y, 630, 50).collidepoint(pygame.mouse.get_pos())):
            pygame.draw.rect(screen, uiconfig.HOVER_COLOR,
                             (350+5, history_render_y-5, 640, 60), 0)
            if events.pressmouse:
                menu.open_project(this_history[1])
        screen.blit(src.basic_font_24.render(
            this_history[0], config.ANTIALIAS_FONT, uiconfig.FONT_COLOR), (350+10, history_render_y))
        screen.blit(src.basic_font_12.render(
            this_history[1], config.ANTIALIAS_FONT, uiconfig.SECOND_FONT_COLOR), (350+10, history_render_y+30))
        pygame.draw.rect(screen, uiconfig.DRAWER_COLOR,
                         (350+10, history_render_y+55, 630, 1), 0)
        history_render_y += 60

    # new file button
    screen.blit(src.new_file, (30, 120))
    screen.blit(src.basic_font_36.render(
        l.newf, config.ANTIALIAS_FONT, uiconfig.FONT_COLOR), (30+64+20, 120+10))
    if (events.pressmouse and pygame.Rect(30, 120, 64, 64).collidepoint(pygame.mouse.get_pos())):
        file_path = filedialog.new_project()
        if file_path != "":
            with open(config.DATA_DIR+"/"+"history.txt", "a") as file:
                file.write(file_path+"\n")
            menu.new_project(file_path)
            menu.open_project(file_path)
        else:
            info("ui/start/startui", "user cancel")

    # open file button
    screen.blit(src.open_file, (30, 120+64+20))
    screen.blit(src.basic_font_36.render(
        l.openf, config.ANTIALIAS_FONT, uiconfig.FONT_COLOR), (30+64+20, 120+10+64+20))
    if (events.pressmouse and pygame.Rect(30, 120+64+20, 64, 64).collidepoint(pygame.mouse.get_pos())):
        file_path = filedialog.open_project()
        if file_path != "":
            with open(config.DATA_DIR+"/"+"history.txt", "a") as file:
                file.write(file_path+"\n")
            menu.open_project(file_path)
        else:
            info("ui/start/startui", "user cancel")
